package com.wuwei.tran;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggingSystem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;
import java.net.URL;

@SpringBootApplication
@Slf4j
public class TranApplication {

    public static void main(String[] args) {


        // 因为springboot默认是不支持图形界面的，设置系统属性，启用图形界面
        System.setProperty("java.awt.headless", "false");

        TranApplication application = new TranApplication();

        application.initSystemTray();

        SpringApplication.run(TranApplication.class, args);
    }

    /**
     * 初始化系统托盘
     */
    @SneakyThrows
    private void initSystemTray() {

        log.error("12345");

        if (SystemTray.isSupported()) {
            URL resource = this.getClass().getClassLoader().getResource("startcms.png");
            ImageIcon imageIcon = new ImageIcon(resource,"photo");
            Image image = imageIcon.getImage();
            TrayIcon trayIcon = new TrayIcon(image);
            trayIcon.setImageAutoSize(true);
            trayIcon.setToolTip("网盘系统");
            //设置托盘菜单
            PopupMenu pm = new PopupMenu() ;
            MenuItem mi = new MenuItem("Exit") ;
            MenuItem mi2 = new MenuItem("Enter");


            ActionListener listener = new ActionListener() {
                @SneakyThrows
                @Override
                public void actionPerformed(ActionEvent e) {
                    // 处理菜单点击事件
                    if (e.getActionCommand().equals("Exit")) {
                        // 点击了Exit菜单

                        String cmdCommand1 = "cmd cd nginx-1.25.4";

                        Runtime.getRuntime().exec(cmdCommand1);

                        String cmdCommand2 = "cmd nginx -s stop";

                        Runtime.getRuntime().exec(cmdCommand2);

                        System.exit(0);
                    } else if (e.getActionCommand().equals("Enter")) {
                        // 点击了Enter菜单
                        // 执行相应的操作


                        URI uri = new URI("http://localhost");

                        Desktop desktop = Desktop.getDesktop();

                        desktop.browse(uri);

                    }
                }
            };

            pm.add(mi);
            pm.add(mi2);

            mi.addActionListener(listener);
            mi2.addActionListener(listener);
            trayIcon.setPopupMenu(pm);
            SystemTray.getSystemTray().add(trayIcon);
        } else {
            System.out.println("系统不支持托盘");
        }
    }

}
